package marchsoft.modules.four.h5.service.Impl;

import lombok.RequiredArgsConstructor;
import marchsoft.modules.four.common.entity.PracticeRecord;
import marchsoft.modules.four.common.entity.Repository;
import marchsoft.modules.four.common.entity.SafeType;
import marchsoft.modules.four.h5.mapper.RepositoryMapper;
import marchsoft.modules.four.h5.service.RepositoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@RequiredArgsConstructor
public class RepositoryServicceImpl implements RepositoryService {

    private final RepositoryMapper mapper;
    private final RepositoryMapper mapperPracticeRecord;

    @Transactional(rollbackFor = Exception.class)
    @Override//前台获取所有安全分类
    public List<SafeType> selectAllSafeType() {
        return mapper.select();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override//按照分类id获取相应的题库
    public List<Repository> selectRepository(Integer id) {
        return mapper.selectRepositoryById(id);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override//新增练习记录。修改练习记录
    public String PracticeRecord(PracticeRecord practiceRecord) {
        boolean f = true;
        boolean j = true;
        boolean k = true;
        String p = ",";
        String FRID = "";
        String FQAn = "";
        System.out.println(practiceRecord);
        Long in = practiceRecord.getUserId();
        int z = in.intValue();
        List<PracticeRecord> all = mapper.selectAllRecord((z));
        for (PracticeRecord record : all) {
            if (practiceRecord.getUserId().equals(record.getUserId())
                    && practiceRecord.getSafeTypeId().equals(record.getSafeTypeId())) {
//                如果存在第n次做重复的题型，需要判断

//                重做题目答案
                String an = record.getFinishQuesAnswer();
                String[] ana = an.split(",");
                String anan = practiceRecord.getFinishQuesAnswer();

//                System.out.println("原答案"+an);
//                System.out.println("现答案"+anan);
//                重做题目
                String repeat = record.getFinishRepoIds();
                String[] s = repeat.split(",");
                for (int b = 0; b < s.length; b++) {
                    if (practiceRecord.getFinishRepoIds().equals(s[b])) {
                        j = false;
                        if ("".equals(ana[b])) {
                            ana[b] = anan;
                            k = false;
                        }
                    }
                }
                String result = String.join(",", ana);
                if (j) {
                    FRID = record.getFinishRepoIds() + p + practiceRecord.getFinishRepoIds();//题目集合
                    FQAn = record.getFinishQuesAnswer() + p + practiceRecord.getFinishQuesAnswer();//答案集合
                } else {
                    FRID = record.getFinishRepoIds();
                    if (k) {
                        FQAn = an;
                    } else {
                        FQAn = result;
                    }
                }
                PracticeRecord newPR = new PracticeRecord(practiceRecord);
                newPR.setId(record.getId());
                newPR.setFinishRepoIds(FRID);
                newPR.setFinishQuesAnswer(FQAn);
                mapperPracticeRecord.updateById(newPR);
                f = false;
            }
        }
        if (f) {
            mapperPracticeRecord.insert(practiceRecord);
        }
        return "true";
    }

}
